home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1991 / 05 / d_flat / applicat.c next >
Text File  |  1991-02-19  |  8KB  |  328 lines

  1. /* ------------- applicat.c ------------- */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "dflat.h"
  7.  
  8. static WINDOW mnu = NULLWND;
  9. extern DBOX TabStops;
  10.  
  11. static void ChooseWindow(WINDOW, int);
  12. static void SelectColors(void);
  13. static void CloseAll(WINDOW);
  14.  
  15. int ApplicationProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  16. {
  17.     int rtn;
  18.     static int tabstop;
  19.  
  20.     switch (msg)    {
  21.         case CREATE_WINDOW:
  22.             LoadConfig();
  23.             SelectColors();
  24.             rtn = BaseWndProc(APPLICATION, wnd, msg, p1, p2);
  25.             if (wnd->extension != NULL)    {
  26.                 mnu = CreateWindow(MENUBAR,
  27.                                     NULL,
  28.                                     GetLeft(wnd)+1,
  29.                                     GetTop(wnd)+1,
  30.                                     1,
  31.                                     WindowWidth(wnd)-2,
  32.                                     NULL,
  33.                                     wnd,
  34.                                     NULL,
  35.                                     0);
  36.                 SendMessage(mnu, BUILDMENU, (PARAM) wnd->extension, 0);
  37.                 SelectColors();
  38.                 AddAttribute(wnd, HASMENUBAR);
  39.                 SendMessage(mnu, SETFOCUS, TRUE, TRUE);
  40.                 if (cfg.InsertMode)
  41.                     SetCommandToggle(ID_INSERT);
  42.                 if (cfg.WordWrap)
  43.                     SetCommandToggle(ID_WRAP);
  44.             }
  45.             PostMessage(wnd, CAPTURE_CLOCK, 0, 0);
  46.             return rtn;
  47.         case SIZE:
  48.             BaseWndProc(APPLICATION, wnd, msg, p1, p2);
  49.             SendMessage(mnu, HIDE_WINDOW, 0, 0);
  50.             SendMessage(mnu, SIZE, GetRight(wnd)-1, GetTop(wnd)+1);
  51.             SendMessage(mnu, BUILDMENU, (PARAM) wnd->extension, 0);
  52.             SendMessage(mnu, SHOW_WINDOW, 0, 0);
  53.             return TRUE;
  54.         case KEYBOARD:
  55.             if (WindowSizing || WindowMoving)
  56.                 break;
  57.             PostMessage(mnu, msg, p1, p2);
  58.             return TRUE;
  59.         case SETFOCUS:
  60.             return FALSE;
  61.         case PAINT:
  62.             if (isVisible(wnd))
  63.                 ClearWindow(wnd, (RECT *)p1, APPLCHAR);
  64.             return TRUE;
  65.         case COMMAND:
  66.             switch ((int)p1)    {
  67.                 case ID_EXIT:
  68.                 case ID_SYSCLOSE:
  69.                     PostMessage(wnd, CLOSE_WINDOW, 0, 0);
  70.                     break;
  71.                 case ID_COLOR:
  72.                     if (!cfg.mono)
  73.                         break;
  74.                     cfg.mono = FALSE;
  75.                     SelectColors();
  76.                     SendMessage(mnu, BUILDMENU, (PARAM) wnd->extension, 0);
  77.                     SendMessage(wnd, SHOW_WINDOW, 0, 0);
  78.                     break;
  79.                 case ID_MONO:
  80.                     if (cfg.mono)
  81.                         break;
  82.                     cfg.mono = TRUE;
  83.                     SelectColors();
  84.                     SendMessage(mnu, BUILDMENU, (PARAM) wnd->extension, 0);
  85.                     SendMessage(wnd, SHOW_WINDOW, 0, 0);
  86.                     break;
  87.                 case ID_TABS:
  88.                     tabstop = cfg.Tabs;
  89.                     switch (tabstop)    {
  90.                         case 2:
  91.                             PushRadioButton(&TabStops, ID_TAB2);
  92.                             break;
  93.                         case 4:
  94.                             PushRadioButton(&TabStops, ID_TAB4);
  95.                             break;
  96.                         case 6:
  97.                             PushRadioButton(&TabStops, ID_TAB6);
  98.                             break;
  99.                         case 8:
  100.                             PushRadioButton(&TabStops, ID_TAB8);
  101.                             break;
  102.                         default:
  103.                             break;
  104.                     }
  105.                     if (DialogBox(&TabStops, NULL))
  106.                         cfg.Tabs = tabstop;
  107.                     break;
  108.                 case ID_TAB2:
  109.                     tabstop = 2;
  110.                     break;
  111.                 case ID_TAB4:
  112.                     tabstop = 4;
  113.                     break;
  114.                 case ID_TAB6:
  115.                     tabstop = 6;
  116.                     break;
  117.                 case ID_TAB8:
  118.                     tabstop = 8;
  119.                     break;
  120.                 case ID_SAVEOPTIONS:
  121.                     SaveConfig();
  122.                 case ID_WINDOW:
  123.                     ChooseWindow(wnd, (int)p2);
  124.                     break;
  125.                 case ID_CLOSEALL:
  126.                     CloseAll(wnd);
  127.                     break;
  128.                 case ID_SYSRESTORE:
  129.                 case ID_SYSMOVE:
  130.                 case ID_SYSSIZE:
  131.                 case ID_SYSMINIMIZE:
  132.                 case ID_SYSMAXIMIZE:
  133.                     return BaseWndProc(APPLICATION, wnd, msg, p1, p2);
  134.                 default:
  135.                     if (inFocus != mnu)
  136.                         PostMessage(inFocus, msg, p1, p2);
  137.                     break;
  138.             }
  139.             return TRUE;
  140.         case LEFT_BUTTON:    {
  141.             WINDOW wnd1 = wnd;
  142.             int mx, my;
  143.             if (WindowSizing || WindowMoving)
  144.                 return FALSE;
  145.             if (SendMessage(wnd, INSIDE_WINDOW, p1, p2))    {
  146.                 if (inFocus && inFocus != mnu)
  147.                     if (SendMessage(inFocus, INSIDE_WINDOW, p1, p2))
  148.                         wnd1 = inFocus;
  149.                 mx = (int) p1 - GetLeft(wnd1);
  150.                 my = (int) p2 - GetTop(wnd1);
  151.                 if (HitControlBox(wnd1, mx, my))    {
  152.                     BuildSystemMenu(wnd1);
  153.                     return TRUE;
  154.                 }
  155.             }
  156.             break;
  157.         }
  158.         case CLOCKTICK:    {
  159.             WINDOW wnd1 = Focus.LastWindow;
  160.             int x = GetLeft(wnd)+WindowWidth(wnd)-9;
  161.             while (wnd1 != NULLWND && wnd1 != wnd)    {
  162.                 if (SendMessage(wnd1, INSIDE_WINDOW, x, GetTop(wnd)))
  163.                     return TRUE;
  164.                 if (SendMessage(wnd1, INSIDE_WINDOW, x+5, GetTop(wnd)))
  165.                     return TRUE;
  166.                 wnd1 = PrevWindow(wnd1);
  167.             }
  168.             foreground = cfg.clr.TitleFG;
  169.             background = cfg.clr.TitleBG;
  170.             wputs(wnd, (char *)p1, WindowWidth(wnd)-9, 0);
  171.             return TRUE;
  172.         }
  173.         case CLOSE_WINDOW:
  174.             SendMessage(NULLWND, RELEASE_CLOCK, 0, 0);
  175.             PostMessage(NULLWND, STOP, 0, 0);
  176.             break;
  177.         default:
  178.             break;
  179.     }
  180.     return BaseWndProc(APPLICATION, wnd, msg, p1, p2);
  181. }
  182.  
  183. void PrepEditMenu(void *wnd, struct Menu *mnu)
  184. {
  185.     struct PopDown *pd = mnu->Selections;
  186.     while (pd->SelectionTitle != NULL)    {
  187.         if (*pd->SelectionTitle != LINE)    {
  188.             switch (pd->ActionId)    {
  189.                 case ID_CUT:
  190.                 case ID_COPY:
  191.                 case ID_CLEAR:
  192.                 case ID_DELETETEXT:
  193.                     if (GetClass(inFocus) == EDITBOX &&
  194.                         isMultiLine(inFocus) &&
  195.                             BlockMarked(inFocus))
  196.                         pd->Attrib &= ~INACTIVE;
  197.                     else
  198.                         pd->Attrib |= INACTIVE;
  199.                     break;
  200.                 case ID_PARAGRAPH:
  201.                     if (GetClass(inFocus) == EDITBOX &&
  202.                             isMultiLine(inFocus))
  203.                         pd->Attrib &= ~INACTIVE;
  204.                     else
  205.                         pd->Attrib |= INACTIVE;
  206.                     break;
  207.                 case ID_PASTE:
  208.                     if (GetClass(inFocus) == EDITBOX &&
  209.                             isMultiLine(inFocus) &&
  210.                                 !TestAttribute(inFocus, READONLY) &&
  211.                                     Clipboard != NULL)
  212.                         pd->Attrib &= ~INACTIVE;
  213.                     else
  214.                         pd->Attrib |= INACTIVE;
  215.                     break;
  216.                 case ID_UNDO:
  217.                     if (GetClass(inFocus) == EDITBOX &&
  218.                             inFocus->DeletedText != NULL)
  219.                         pd->Attrib &= ~INACTIVE;
  220.                     else
  221.                         pd->Attrib |= INACTIVE;
  222.                     break;
  223.                 default:
  224.                     break;
  225.             }
  226.         }
  227.         pd++;
  228.     }
  229. }
  230.  
  231. void PrepOptionsMenu(void *wnd, struct Menu *mnu)
  232. {
  233.     struct PopDown *pd = FindCmd(ID_TABS);
  234.     sprintf(pd->SelectionTitle+7, "%d", cfg.Tabs);
  235. }
  236.  
  237. static char *Menus[9] = {
  238.     "~1.                      ",
  239.     "~2.                      ",
  240.     "~3.                      ",
  241.     "~4.                      ",
  242.     "~5.                      ",
  243.     "~6.                      ",
  244.     "~7.                      ",
  245.     "~8.                      ",
  246.     "~9.                      "
  247. };
  248.  
  249. void PrepWindowMenu(void *wnd, struct Menu *mnu)
  250. {
  251.     struct PopDown *pd = mnu->Selections;
  252.     struct PopDown *ca = mnu->Selections + 12;
  253.     int MenuNo = 0;
  254.     WINDOW wnd1 = Built.FirstWindow;
  255.     mnu->Selection = 0;
  256.     while (wnd1 != NULLWND && MenuNo < 9)    {
  257.         if (GetClass(wnd1) != MENUBAR && GetParent(wnd1) == wnd)    {
  258.             strncpy(Menus[MenuNo]+4, GetTitle(wnd1), 20);
  259.             pd->SelectionTitle = Menus[MenuNo];
  260.             if (wnd1 == inFocus)    {
  261.                 pd->Attrib |= CHECKED;
  262.                 mnu->Selection = MenuNo;
  263.             }
  264.             else
  265.                 pd->Attrib &= ~CHECKED;
  266.             pd++;
  267.             MenuNo++;
  268.         }
  269.         wnd1 = NextWindowBuilt(wnd1);
  270.     }
  271.     if (MenuNo)    {
  272.         *pd++ = *ca++;
  273.         *pd++ = *ca;
  274.     }
  275.     pd->SelectionTitle = NULL;
  276. }
  277.  
  278. static void ChooseWindow(WINDOW wnd, int WindowNo)
  279. {
  280.     WINDOW wnd1 = GetFirstChild(wnd);
  281.     while (wnd1 != NULLWND)    {
  282.         if (GetClass(wnd1) != MENUBAR)
  283.             if (WindowNo-- == 0)
  284.                 break;
  285.         wnd1 = GetNextChild(wnd);
  286.     }
  287.     if (inFocus->condition == ISMAXIMIZED)
  288.         SendMessage(inFocus, RESTORE, 0, 0);
  289.     SendMessage(wnd1, SETFOCUS, TRUE, 0);
  290.     if (wnd1->condition == ISMINIMIZED)
  291.         SendMessage(wnd1, RESTORE, 0, 0);
  292. }
  293.  
  294. static void SelectColors(void)
  295. {
  296.     if (ismono() || video_mode == 2)    {
  297.         cfg.mono = TRUE;
  298.         DeactivateCommand(ID_COLOR);
  299.     }
  300.     if (cfg.mono)    {
  301.         cfg.clr = bw;
  302.         SetCommandToggle(ID_MONO);
  303.         ClearCommandToggle(ID_COLOR);
  304.     }
  305.     else    {
  306.         cfg.clr = color;
  307.         SetCommandToggle(ID_COLOR);
  308.         ClearCommandToggle(ID_MONO);
  309.     }
  310. }
  311.  
  312. static void CloseAll(WINDOW wnd)
  313. {
  314.     int closing = TRUE;
  315.     while (closing)    {
  316.         WINDOW wnd1 = GetLastChild(wnd);
  317.         closing = FALSE;
  318.         while (GetClass(wnd1) != MENUBAR)    {
  319.             if (wnd1 == inFocus)    {
  320.                 SendMessage(wnd1, CLOSE_WINDOW, 0, 0);
  321.                 closing = TRUE;
  322.                 break;
  323.             }
  324.             wnd1 = GetPrevChild(wnd);
  325.         }
  326.     }
  327. }
  328.